package com.mrx.commons.http;

import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

/**
 * {@link RestTemplate} 配置类
 *
 * @author Mr.X
 * @since 2023-11-17 09:36
 **/
public class HttpConfig {

    @Bean
    public RestTemplate restTemplate() {
        RestTemplate template = new RestTemplate();
        template.getMessageConverters().stream().filter(it -> it instanceof StringHttpMessageConverter)
                .map(it -> (StringHttpMessageConverter) it).findFirst()
                .ifPresent(it -> it.setDefaultCharset(StandardCharsets.UTF_8));
        return template;
    }

    @Bean
    public RestTemplateUtils restTemplateUtils() {
        return new RestTemplateUtils();
    }

}
